package com.leo;

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @ClassName ButtonDemo
 * @Description button 展示
 * @Author wangss
 * @date 2020.05.16 22:17
 * @Version 1.0
 */
public class ButtonDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button();
        button.setText("这是按钮");

        // 设置按钮在group中的xy位置
        button.setLayoutX(10d);
        button.setLayoutY(10d);

        // 设置按钮的大小
        button.setPrefHeight(50d);
        button.setPrefWidth(200d);

        // 设置button的字体
        button.setFont(Font.font(30d));
        button.setTextFill(Paint.valueOf("#CD0000"));

        // 设置button的背景：颜色、圆角、按钮到边框的距离（类似padding）
        // RGB #8FBC8F 可以写成 #8FBC8F00 最后两位代表透明度
        BackgroundFill backgroundFill = new BackgroundFill(Paint.valueOf("#8FBC8F"), new CornerRadii(20), new Insets(10));
        Background background = new Background(backgroundFill);
        button.setBackground(background);

        // 设置button的边框
        BorderStroke borderStroke = new BorderStroke(Paint.valueOf("#8A2BE2"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(5));
        Border border = new Border(borderStroke);
        button.setBorder(border);

        final Group group = new Group();
        // 增加组件
        group.getChildren().addAll(button);

        // 监听
        group.getChildren().addListener(new ListChangeListener<Node>() {
            @Override
            public void onChanged(Change<? extends Node> c) {
                System.out.println("当前子组件数量 = " + c.getList().size());
            }
        });

        Scene scene = new Scene(group);

        primaryStage.setScene(scene);
        primaryStage.setTitle("button");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.show();
    }
}
